let imp = require('../../../utils/util')
import Toast from '@vant/weapp/toast/toast';
import {
    request
} from '../../../utils/api.js';
import {
    sexcolumns,
    politicscolumns
} from '../../../utils/columus.js';
Page({
    data: {
        userId: 0,
        sub: "submit",
        show: false,
        showDate: false,
        showpoliticalOutlook: false,
        form: {},
        currentDate: new Date().getTime(),
        maxDate: new Date().getTime(),
        minDate: new Date(1900, 1, 1).getTime(),
        columns: sexcolumns,
        columns1: politicscolumns,
        fileList: [],
        fileList1: [],
        message: [''],
        formatter: imp.formatter,
        fieldValue: '',
        cascaderValue: '',
        user: null,
        flag: false,
    },
    afterRead(e) {
        let index = e.currentTarget.dataset.index
        if (index === '1') {
            this.setData({
                fileList: [{
                    url: e.detail.file.tempFilePath
                }]
            })
        } else if (index === '2') {
            this.setData({
                fileList1: [{
                    url: e.detail.file.tempFilePath
                }]
            })
        }
    },
    deleteImg(e) {
        let index = e.currentTarget.dataset.index
        if (index === '1') {
            this.setData({
                fileList: []
            })
        } else if (index === '2') {
            this.setData({
                fileList1: []
            })
        }
    },
    open(e) {
        let field = e.currentTarget.dataset.num
        this.setData({
            [field]: !this.data[field],
        });
    },

    cancel() {
        this.setData({
            show: false,
            showDate: false,
            showpoliticalOutlook: false,
        });
    },

    change(e) {
        let field = e.currentTarget.dataset.num
        this.setData({
            [`form.${[field]}`]: e.detail
        });
    },

    dateConfirm(event) {
        this.setData({
            currentDate: event.detail,
            ['form.birthday']: imp.formatTime(new Date(event.detail)).split(' ')[0]
        });
        this.cancel()
    },

    sexConfirm(event) {
        this.setData({
            ['form.sexName']: event.detail.value.text,
            ['form.sex']: event.detail.value.id
        })
        this.cancel()
    },

    politicalOutlookConfirm(event) {
        this.setData({
            ['form.politicalOutlookName']: event.detail.value.text,
            ['form.politicalOutlook']: event.detail.value.id
        })
        this.cancel()
    },

    async res() {
        this.setData({
            message: []
        })
        let {
            form
        } = this.data

        if (!form.name) this.setData({
            'message[0]': '请输入姓名'
        })
        if (!form.sexName) this.setData({
            'message[1]': '请选择性别'
        })
        if (!form.birthday) this.setData({
            'message[2]': '请输入姓名'
        })
        if (!form.nativePlace) this.setData({
            'message[3]': '请输入姓名'
        })
        if (!form.schoolId) this.setData({
            'message[4]': '请输入姓名'
        })
        if (!form.politicalOutlookName) this.setData({
            'message[5]': '请输入姓名'
        })
        if (!form.lessonId) this.setData({
            'message[6]': '请输入姓名'
        })
        if (!form.phone) this.setData({
            'message[7]': '请输入姓名'
        })
        if (!this.data.fileList[0]) {
            Toast.fail("未上传上传照片")
            return
        }
        if (!this.data.fileList1[0]) {
            Toast.fail("未上传上传意向受助信息表")
            return
        }

        let res = await request({
            url: '/user/edit',
            method: 'post',
            data: this.data.form
        })
        if (res.code == 200) {
            const user = wx.getStorageSync('user')
            if (user.userId == this.data.userId) {
                let result = await request({
                    url: '/user/getUserInfoById?userId=' + this.data.userId,
                    method: 'get'
                })
                wx.setStorageSync('user', result.result)
            }

            if (this.data.form.userType == 0 && !this.data.flag) {
                wx.navigateTo({
                    url: '/pages/details/family/family?id=' + this.data.userId,
                })
            } else {
                Toast.success('修改成功');
                setTimeout(() => {
                    wx.navigateBack()
                }, 600);
            }
        } else {
            Toast.console.error('修改失败');
        }
    },

    next() {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        if (options.flag) {
            this.setData({
                flag: true
            })
        } else {
            this.setData({
                flag: false
            })
        }
        this.setData({
            userId: options.id
        })
        let result = await request({
            url: '/user/getUserInfoById?userId=' + this.data.userId,
            method: 'get'
        })

        if (result.result) {
            let form = result.result
            console.log(result);
            this.setData({
                form
            })
            console.log(this.data.form);
            let politicalOutlook = this.data.columns1.filter(item => {
                if (item.id == form.politicalOutlook) {
                    return item
                }
            })
            this.setData({
                ['form.sexName']: form.sex == 1 ? '男' : '女',
                ['form.politicalOutlookName']: politicalOutlook[0].text
            })
            this.setData({
                ['form.fileUrl']: 'https://img.yzcdn.cn/vant/cat.jpeg',
                ['form.photoUrl']: 'https://img.yzcdn.cn/vant/cat.jpeg',
                fileList: [{
                    url: 'https://img.yzcdn.cn/vant/cat.jpeg'
                }],
                fileList1: [{
                    url: 'https://img.yzcdn.cn/vant/cat.jpeg'
                }]
            })

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})